/***     
 * SP_TWO_DOUBLE: 3, // 两对牌型
 */

var CardGroupType = require('./CardGroupType');
var FindSameCardValueTool = require('./../FindTool/FindSameCardValueTool');
var _ = require('./../Underscore');
var CardType = require('./../CardType');
var CardTool = require('./../CardTool');
var FindShunZiTool = require('../FindTool/FindShunZiTool');
var FindPairsTool = require('./../FindTool/FindPairsTool');
var GroupTwoPairs = module.exports = {};


GroupTwoPairs.getName = function() {
    return '两对牌型';
};

GroupTwoPairs.getType = function() {
    return CardGroupType.SP_TWO_DOUBLE;
};

GroupTwoPairs.getMoreResult = function(_cardInfoList) {
    var resultInfo = FindPairsTool.getResult(_cardInfoList, 1);
    var result = {
        isFind: false,
        findGroupMore: []
    };

    if (resultInfo.isFind) {
        var size = _.size(resultInfo.findGroup);
        if (size < 2) {
            CardTool.LogResult(result, this.getName());
            result.findGroupMore.push({
                findGroup: [],
                otherGroup: _cardInfoList
            });
            return result;
        } else if (size == 2) {
            result.isFind = true;
            var info = this.autoAddACard(_cardInfoList, resultInfo.findGroup);
            result.findGroupMore.push(info);
            CardTool.LogResult(result, this.getName());
            return result;
        } else {
            result.isFind = true;
            var findGroup = resultInfo.findGroup;
            for (var i = 0; i < (size - 1); ++i) {
                //第一对 
                for (var j = (i + 1); j < size; ++j) {
                    var fristPairs = _.clone(findGroup[i]);
                    var secondGroup = findGroup[j];
                    fristPairs.push(secondGroup);
                    var info = this.autoAddACard(_cardInfoList, fristPairs);
                    result.findGroupMore.push(info);
                    // break;
                }
            }
            CardTool.LogResult(result, this.getName());
            return result;
        }
    }
    CardTool.LogResult(result, this.getName());
    return result;
};

/***组合2对+1的牌 */
GroupTwoPairs.autoAddACard = function(_cardInfoList, findGroup) {
    var tmpFindGroup = _.flatten(findGroup);
    var otherGroup = _.difference(_cardInfoList, tmpFindGroup);
    otherGroup = _.sortBy(otherGroup, function(_cardInfo) {
        return _cardInfo.value;
    });
    var lastIdx = _.size(otherGroup) - 1;
    tmpFindGroup.push(otherGroup[lastIdx]);

    otherGroup = _.initial(otherGroup);

    var info = {
        findGroup: tmpFindGroup,
        otherGroup: otherGroup
    }
    return info;
};